/* This funtion fired when a link / button is clicked
* It change the hidden field named action's value to $param _action
* and then submit the form $param _formName
*/
function submitButton(_action, _formName)
{
	$("form#"+_formName+" input#action").val(_action);
	$("form#"+_formName).submit();
	return false;
}

function submitMultiple($_trigger, _gridName)
{
	$.ajax({'async':false,'cache':false,'type':'POST',
		'url':$_trigger.attr('data-href'),
		'data':$('input[name*="multiselect"]').serialize(),
		'success':function(data)
		{
			data = $.parseJSON(data);
			if(data['result']=='error') alert(data['message']);
			else if(data['result']=='success')
			{
				$('.flash-success, .flash-error').html('');
				if(data['success_ids']!='') {
					$('.flash-success').html(data['success_ids']);
					$.fn.yiiGridView.update(_gridName);
				}
				if(data['error_ids']!='') {
					$('.flash-error').html(data['error_ids']);
				}
			}
		},
		'error':function(data){alert(data.responseText);}
	});
	return false;
}

function showHelp(_help_id)
{
	alert(_help_id);
	return false;
}


/*
$(document).ready(function(){
	$('.form .row.sticky input:not(.error), .form .row.sticky select:not(.error), .form .row.sticky textarea:not(.error)').each(function(){
		var value;
		if(this.tagName=='SELECT')
			value=this.options[this.selectedIndex].text;
		else if(this.tagName=='TEXTAREA')
			value=$(this).html();
		else
			value=$(this).val();
		if(value=='')
			value='[empty]';
		$(this).before('<div class="value">'+value+'</div>').hide();
	});

	$('.form .row.sticky .value').live('click', function(){
		$(this).hide();
		$(this).next().show().get(0).focus();
	});
});

function ajaxLinkTrigger($_trigger, _callback)
{
	$.ajax({
		url:$_trigger.attr('href'),
		context:$_trigger,
		success:_callback
	});
	return false;
}

function ajaxInlineTrigger($_selector, $_trigger, _callback)
{
	$_selector.on('click', $_trigger, function(){
		$trigger = $(this);
		$trigger.hide();
		switch($trigger.attr('data-type'))
		{
			case 'textField':
				$input=$('<input type="text" />');
				break;
			case 'textArea':
				$input=$('<textarea></textarea>');
				break;
		}
		$trigger.after($input);		
		
		$input.attr('size',$trigger.attr('data-width'));
		$input.val($.trim($trigger.html())).on('keypress',function(event){
			if(event.keyCode==13 && ($(this).is("input") || (event.ctrlKey && $(this).is("textarea"))))
			{
				$(this).trigger('blur');
			}
			else if(event.keyCode==27)
			{
				$(this).val($trigger.html());
				$(this).trigger('blur');
			}
		}).one('blur',function(){
			if($(this).val()==$trigger.html()) {
				$trigger.html($input.val()).show();
				$input.remove();
				return;
			}
			
			$(this).attr('disabled','disabled').after($loading=$('<span class="loading-s1"></span>'));
			$.ajax({
				url:$trigger.attr('data-href'),
				type:'POST',
				async:false,
				data:{_value:$(this).val()},
				context:$(this),
				success:function(data){
					data = $.parseJSON(data);
					if(data.result=='success')
					{
						$trigger.html(data.value).show();
						$input.remove();
					}
					else
					{
						alert(data.result);
					}
				}
			});
		}).focus();
	});
}

*/
